﻿using System;
using System.Linq;
using System.Web.UI.WebControls;

namespace ECommerce.Administration.Controls
{
	public partial class CustomerCashTicketList : AdminBaseUsercontrol
	{
		private Service.ICashTicketService _CashTicketService;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				ButtonSearch.Text = GetLocalResourceString("Admin.SearchButton.Text");
			}
		}

		protected void GridCustCashTicket_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.Header)
			{
				e.Row.Cells[0].Text = GetLocalResourceString("Admin.CustomerCashTicketList.UserNameHeader");
				e.Row.Cells[1].Text = GetLocalResourceString("Admin.CustomerCashTicketList.EmailHeader");
				e.Row.Cells[2].Text = GetLocalResourceString("Admin.CustomerCashTicketList.CashTicketHeader");
			}

			if (e.Row.DataItem != null)
			{
				var RepeaterCashTickets = e.Row.FindControl("RepeaterCashTickets") as Repeater;
				var userID = GridCustCashTicket.DataKeys[e.Row.RowIndex]["ProviderUserKey"] as Guid?;
				var cashticketinputcontainer = e.Row.FindControl("cashticketinputcontainer") as System.Web.UI.HtmlControls.HtmlContainerControl;
				var ButtonSave = e.Row.FindControl("ButtonSave") as Button;

				if (userID.HasValue)
				{
					if (_CashTicketService == null)
					{
						_CashTicketService = new Service.CashTicketService();
					}

					System.Collections.Generic.IEnumerable<Data.Entity.CashTicket> cashTickets = _CashTicketService.GetMyCashTicket(userID.Value);

					if(cashTickets != null)
					{
						cashTickets = cashTickets.Where(exp => exp.IsUsed == 0);
					}

					RepeaterCashTickets.DataSource = cashTickets;
					RepeaterCashTickets.DataBind();
					ButtonSave.Text = GetLocalResourceString("Admin.SaveButtonText");
				}
			}
		}

		protected void GridCustCashTicket_RowUpdating(object sender, GridViewUpdateEventArgs e)
		{
			var TextCashTicketCost = GridCustCashTicket.Rows[e.RowIndex].FindControl("TextCashTicketCost") as TextBox;
			var userID = GridCustCashTicket.DataKeys[e.RowIndex]["ProviderUserKey"] as Guid?;

			decimal cost = decimal.Zero;
			if (decimal.TryParse(TextCashTicketCost.Text.Trim(), out cost))
			{
				var theNewCashTicket = new Data.Entity.CashTicket() { Cost = cost, CreatedOn = DateTime.Now, IsUsed = 0, UserID = userID.Value };

				if (_CashTicketService == null)
				{
					_CashTicketService = new Service.CashTicketService();
				}

				if (_CashTicketService.CreateNewCashTicket(theNewCashTicket) > 0)
				{
					BindUsers();
				}
			}
		}

		protected void ButtonSearch_Click(object sender, EventArgs e)
		{
			BindUsers();
		}

		private void BindUsers()
		{
			System.Web.Security.MembershipUserCollection membershipUsers = new System.Web.Security.MembershipUserCollection();

			if (!string.IsNullOrEmpty(TextUserName.Text))
			{
				GridCustCashTicket.DataSource = System.Web.Security.Membership.FindUsersByName(TextUserName.Text + "%");
				GridCustCashTicket.DataBind();
			}
			else
			{
				GridCustCashTicket.DataSource = membershipUsers;
				GridCustCashTicket.DataBind();
			}
		}
	}
}